#!/bin/bash

# $Header$
# Author: yang.jin@ebay.com

export PATH=/bin:/usr/bin:/opt/bind/bin
DNS_SERVERS_LIST="/etc/named/IT_DNS_SERVERS"

if [ $# -ne 0 ]; then 
	echo "
		$(basename $0) - Verify connections from a slave to master DNS servers

		Usage:
			
		$(basename $0)
"
	exit 1	
fi

if [ "X$(uname -s)" == "XSunOS" ]; then
	GREP="/usr/xpg4/bin/grep"
else
	GREP="/bin/grep"
fi

if [ ! -f $DNS_SERVERS_LIST ]; then
        echo -e "The configuration file \"$DNS_SERVERS_LIST\" does not exist."
        exit 2
fi

. $DNS_SERVERS_LIST

# PRIMARY=$(echo $DNS_PRIMARY_MASTER)
PRIMARY="phxitdns01.vip.its.ebay.com"
TESTDOMAIN="vip.its.ebay.com"

local=$(host $(hostname) | awk '/has address/ {print $1}')
localip=$(host $(hostname) | awk '/has address/ {print $4}')
if [ "X${local}" == "X"  -o "X${localip}" == "X" ]; then echo "Error: hostname $(hostname) cannot be resolved." >&2; exit 1; fi

for master in $(echo $DNS_PRIMARY_MASTER $DNS_PSEUDO_MASTERS); do 

	masterip=$(host $master | awk '/has address/ {print $4}')

	echo -e "\nVerify connectdions from ${local}(${localip}) to master ${master}(${masterip})-"


	echo -e "verifying 53/TCP with telnet...\t\t\c"

	if echo quit | telnet $master 53 2>/dev/null | $GREP -q -E  "^Escape character is '\^]'\."; then
		echo Passed
		echo -e "verifying 53/TCP with dig...\t\t\c"
		if dig @${master} -t soa $TESTDOMAIN +tcp +short | $GREP -q "^${PRIMARY}"; then
			echo Passed
		else
			echo Failed
		fi
	else
		echo Failed
	fi

	echo -e "verifying 53/UDP with dig...\t\t\c"
	if dig @${master} -t soa $TESTDOMAIN +notcp +short | $GREP -q "^${PRIMARY}"; then
		echo Passed
	else
		echo Failed
	fi
done
